#include <stdio.h>
#include <string.h>

// 对指针数组进行排序
extern void sort(char *name[], int n);

int main(int argc, char *argv[])
{
    char *names[4];
    names[0] = "A-Follow";
    names[1] = "D-Great";
    names[2] = "C-NUAA";
    names[3] = "B-Hello!";
    // 传入一个指针数组
    sort(names, 4);
    printf("names[0]=%s\n", names[0]);
    printf("names[1]=%s\n", names[1]);
    printf("names[2]=%s\n", names[2]);
    printf("names[3]=%s\n", names[3]);
    return 0;
}

void sort(char *name[], int n)
{
    // 选择排序框架
    int i = 0, j = 0;
    for(i = 0; i < n - 1; i++)
    {
        int current_min_index = i;
        for(j = i + 1; j < n; j++)
        {
            // name[j]和name[current_min_index]都是指向字符串的指针
            // 可以直接传给strcmp函数进行比较
            if (strcmp(name[j], name[current_min_index]) < 0)
                current_min_index = j;
        }
        // 交换指针数组元素name[i]与name[current_min_index];
        char *tmp = name[i]; // tmp也是一个指针变量
        name[i] = name[current_min_index];
        name[current_min_index] = tmp;
    }
}
